<?php
include_once('include/common.inc.php');
include_once(ONEZ_ROOT.'/codes/'.CODE.'.php');
$action=GP('action');
switch($action){
  case'code':
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pramga: no-cache');
    $token=md5(onlineip().uniqid());
    include_once(ONEZ_ROOT.'/source/code.php');
    break;
  case'uc_setup':
    include_once(ONEZ_ROOT.'/source/uc_setup.php');
    break;
  case'test':
    $uid='2';
    $cookie=StrCode("$uid","ENCODE");
    _cookie('qraccount',$cookie,86400*180);
  case'verify':
    $data=GP('token');
    $token=substr($data,0,32);
    $tmptoken=substr($data,32);
    $file=ONEZ_ROOT.'/cache/tokens/'.$token;
    !file_exists($file) && exit('此账号的绑定信息已失效');
    $uid=(int)@readover($file);
    !$uid && exit('此账号的绑定信息已失效');
    @touch($file);
    $file=ONEZ_ROOT.'/cache/temps/'.$tmptoken;
    mkdirs(dirname($file));
    writeover($file,StrCode(serialize(array('uid'=>$uid)),'ENCODE'));
    exit('Y');
    break;
  case'synlogin':
    #清除24小时之前的数据
    foreach(glob(ONEZ_ROOT.'/cache/temps/*') as $v){
      if(filemtime($v)<time()-86400){
        @unlink($v);
      }
    }
    
    $token=GP('token');
    !$token && exit('参数非法');
    $cacheFile=ONEZ_ROOT.'/cache/temps/'.$token;
    if(file_exists($cacheFile)){
      $data=@unserialize(StrCode(@readover($cacheFile),'DECODE'));
      onez_call('synlogin');
      @unlink($cacheFile);
    }else{
      exit('N');
    }
    break;
  case'logout':
    foreach(array_keys($_COOKIE) as $k){
      if($k=='PHPSESSID')continue;
      _cookie($k,'del');
      
    }
    header('location:?action=verify&token=none&t='.rand(10000,99999));
    break;
  case'update':
    $token=GP('token','P');
    !$token && exit('参数非法');
    $cacheFile=ONEZ_ROOT.'/cache/temps/'.$token;
    if(file_exists($cacheFile)){
      exit('Y');
    }else{
      exit('N');
    }
    break;
  case'bind':
    $token=GP('token','G');
    $size<4 && $size=4;
    $size>12 && $size=12;
    $file=ONEZ_ROOT.'/cache/temps/'.$token;
    !file_exists($file) && exit('数据已失效[001]');
    $data=@readover($file);
    $A=(array)@unserialize($data);
    (!$A || count($A)!=7) && exit('数据已失效[001]');
    writeover($file,$A[6]);
    include ONEZ_ROOT."/qrcode/qrlib.php";
    Header("Content-type: image/png");
    QRcode::png(implode('_onez_',$A),false,$size,$size);
    break;
  case'icon':
    header('location:'.$homepage.'/icon.png');
    break;
  case'avatar':
    header('location:'.$homepage.'/icon.png');
    break;
  case'touch':
    $uid=GP('uid','G');
    $token=GP('token','G');
    $rndkey=GP('rndkey','G');
    $token!=getToken($uid) && exit('非法请求'.$uid);
    $file=ONEZ_ROOT.'/cache/temps/'.$token;
    !file_exists($file) && exit('数据已失效[001]');
    @readover($file)!=$rndkey && exit('数据已失效[002]');
    @unlink($file);
    
    $file2=ONEZ_ROOT.'/cache/tokens/'.$token;
    mkdirs(dirname($file2));
    writeover($file2,$uid);
    
    echo'Y';
    break;
  case'bindusr':
    include_once(ONEZ_ROOT.'/source/bindusr.php');
    break;
  case'qrcode':
    $token=GP('token','G');
    $size=(int)GP('size','G');
    $size<4 && $size=4;
    $size>12 && $size=12;
    include ONEZ_ROOT."/qrcode/qrlib.php";
    Header("Content-type: image/png");
    QRcode::png('login_onez_'.$homepage.'_onez_'.$token,false,$size,$size);
    break;
  default:
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
    header('Cache-Control: no-cache, must-revalidate');
    header('Pramga: no-cache');
    $token=md5(onlineip().uniqid());
    include_once(ONEZ_ROOT.'/source/index.php');
    break;
}